package com.anecdote.ideaplugins.changesbar;

import com.intellij.openapi.util.IconLoader;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Copyright 2006 Nathan Brown
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

abstract class ConflictPopupPanel extends JPanel
{
  private Action _clickAction;
  private static final Icon HIDE_ICON = IconLoader.getIcon("/com/anecdote/ideaplugins/changesbar/hide.gif");
  private final JButton _titleButton = new JButton(HIDE_ICON);
  private static final Insets INSETS = new Insets(5, 5, 5, 5);

  /**
   * Creates a balloon tip with the parameters provided.
   *
   * @param titleText   The text of the title string
   * @param titleIcon   The icon for the title string
   * @param contentText the text contents of the baloon
   */
  public ConflictPopupPanel(String titleText, Icon titleIcon, String contentText, Action clickAction)
  {
    super(new GridBagLayout());
    setOpaque(false);
    _clickAction = clickAction;

    final JLabel titleLabel = new JLabel(titleText, titleIcon, SwingConstants.LEFT);
    final Font baseFont = titleLabel.getFont();
    titleLabel.setFont(baseFont.deriveFont(Font.BOLD));
    add(titleLabel, new GridBagConstraints(0, 0, 1, 1, 1.0F, 0.0F, GridBagConstraints.WEST,
                                           GridBagConstraints.HORIZONTAL, INSETS, 0, 0));

    _titleButton.setOpaque(false);
    _titleButton.setBorder(BorderFactory.createLineBorder(getForeground()));
    _titleButton.setFocusPainted(false);
    add(_titleButton, new GridBagConstraints(1, 0, 1, 1, 0.0F, 0.0F, GridBagConstraints.CENTER,
                                             GridBagConstraints.NONE, INSETS, 0,
                                             0));
    _titleButton.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        hideContainer();
      }
    });

    final JTextArea contentArea = new JTextArea(contentText);
//    _contentArea = new JLabel("<html>" + ComponentUtilities.createFormattedHTML(contentText, null, baseFont, false));
    contentArea.setFont(baseFont);
    contentArea.setBackground(null);
    contentArea.setBorder(null);
    contentArea.setLineWrap(true);
    contentArea.setWrapStyleWord(true);
    contentArea.setEditable(false);
    contentArea.setCursor(Cursor.getDefaultCursor());
    contentArea.setOpaque(false);
    contentArea.setPreferredSize(new JLabel(contentText).getPreferredSize());
    add(contentArea, new GridBagConstraints(0, 1, 2, 1, 1.0F, 1.0F, GridBagConstraints.NORTHWEST,
                                            GridBagConstraints.BOTH, INSETS, 0,
                                            0));

    final MouseAdapter mouseAdapter = new MouseAdapter()
    {
      @Override
      public void mousePressed(MouseEvent e)
      {
        if (_clickAction != null) {
          _clickAction.actionPerformed(new ActionEvent(this, 0, null));
        }
        hideContainer();
      }
    };
    contentArea.addMouseListener(mouseAdapter);
    titleLabel.addMouseListener(mouseAdapter);
    addMouseListener(mouseAdapter);
    pack();
  }


  protected abstract void hideContainer();

  private void pack()
  {
    setSize(getPreferredSize());
    validate();
    doLayout();
    setSize(getPreferredSize());
    validate();
    setMinimumSize(getPreferredSize());
  }
}